package cn.zqh.user.service.impl;

import cn.zqh.bean.api.CommonResult;
import cn.zqh.bean.api.ResultCode;
import cn.zqh.bean.bo.User;
import cn.zqh.bean.bo.UserPermissionConnect;
import cn.zqh.user.config.BSException;
import cn.zqh.user.mapper.UserMapper;
import cn.zqh.user.mapper.UserPermissionConnectMapper;
import cn.zqh.user.service.UserPermissionConnectService;
import cn.zqh.uuid.utils.SnowflakeId;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zqh
 * @since 2022-12-24
 */
@Service
public class UserPermissionConnectServiceImpl extends ServiceImpl<UserPermissionConnectMapper, UserPermissionConnect> implements UserPermissionConnectService {

    @Resource
    private UserPermissionConnectMapper userPermissionConnectMapper;

    @Resource
    private UserMapper userMapper;



    /**
      * @Author: zqh
      * @Date:
      * @Description: 权限赋予
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult permissionGranting(String userId, Integer permissionId) {
        User user = userMapper.selectById(userId);
        if (user == null){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        UserPermissionConnect userPermissionConnect1 = userPermissionConnectMapper.selectOne(new QueryWrapper<UserPermissionConnect>().eq("user_id", userId).eq("permission_id", permissionId));
        if (userPermissionConnect1 != null){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),"该用户已拥有此权限");
        }
        UserPermissionConnect userPermissionConnect = new UserPermissionConnect();
        userPermissionConnect.setId("Permission"+SnowflakeId.nextId())
                .setUserId(userId)
                .setPermissionId(permissionId);

        int insert = userPermissionConnectMapper.insert(userPermissionConnect);
        if (insert != 1){
            return CommonResult.failed();
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }


    /**
      * @Author: zqh
      * @Date:
      * @Description: 权限回收
    */
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public CommonResult rightsRecycling(String userId, Integer permissionId) {
        User user = userMapper.selectById(userId);
        if (user == null){
            throw new BSException(ResultCode.VALIDATE_FAILED.getCode(),ResultCode.VALIDATE_FAILED.getMessage());
        }
        int i = userPermissionConnectMapper.delete(new QueryWrapper<UserPermissionConnect>().eq("user_id", userId).eq("permission_id", permissionId));
        if (i != 1){
            return CommonResult.failed();
        }
        return CommonResult.success(ResultCode.SUCCESS);
    }
}
